﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Vouch
{
    /// <summary>
    /// 获取客户档案的Url：http://zn.zksoft.cc/api/oms/NotCustomer/GetTop5?name=%E5%AE%9D%E5%8D%8E
    /// </summary>
    public class VouchInitTbCustomer
    {
        private TextBox _tb;
        private ListBox _listbox;
        private Control _main;
        Timer timmer;
        private bool _bEnable = false;

        public VouchInitTbCustomer(Control c)
        {
            _main = c;
            _listbox = new ListBox();
            _listbox.MouseClick += listBox1_MouseClick;
            _listbox.KeyDown += listBox1_KeyDown;
            _listbox.Visible = false;
            _main.Controls.Add(_listbox);
            timmer = new Timer();
            timmer.Interval = 1000;
            timmer.Tick += textboxChange;
            timmer.Stop();
        }

        #region 移除
        public void DisableHandle()
        {
            _bEnable = false;
            timmer.Stop();
            _listbox.Visible = false;
        }
        #endregion

        #region 移除
        public void EnableHandle()
        {
            _bEnable = true;
            timmer.Start();
       }
        #endregion


        #region 初始化
        public void AddHandle(TextBox tb)
        {
            _tb = tb;
            _tb.KeyDown += textBox1_KeyDown;
            _tb.GotFocus += textboxGotFocus;
            _tb.LostFocus += textboxLostFocus;
        }
        #endregion

        private DateTime _lastinit;
        private string _lastvalue;

        #region 值改变
        private void textboxChange(object sender, EventArgs e)
        {
            if (!_bEnable) { return; }
            _main.Text = DateTime.Now.ToString("hh:mm:ss");
            var ts = DateTime.Now - _lastinit;
            if (ts.TotalSeconds < 1) { return; }
            timmer.Stop();
            if (_tb.Text.Trim() != _lastvalue)
            {
                _lastvalue = _tb.Text.Trim();
                var url = string.Format("{0}/api/oms/Customer/GetTop10?token={1}&name={2}",
                ServerConfig.ServerAddress, ServerConfig.GetToken(), _lastvalue);
                var s = Common.handleHttp.Get(url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result == -1) { MessageBox.Show(result.resultdetail); return; }
                var ar = result.GetListDic("customer");
                _listbox.Items.Clear();
                if (ar.Count == 0) { _listbox.Visible = false; }
                for (int i = 0; i < ar.Count; i++)
                {
                    var dic = (Dictionary<string, object>)ar[i];
                    _listbox.Items.Add(dic["cCusName"].ToString());
                }
                _listbox.Left = _tb.Left;
                _listbox.Top = _tb.Top + _tb.Height + 5;
                _listbox.Width = _tb.Width;
                _listbox.Height = _tb.Height * 5;                
                _listbox.Visible = true;
                _listbox.BringToFront();
                _main.Text = DateTime.Now.ToString("hh:mm:ss") +":"+ _listbox.Items.Count.ToString();
                _lastinit = DateTime.Now;
                //if (!_tb.Focused && !_listbox.Focused) { _listbox.Visible = false;timmer.Stop(); }
                //else { timmer.Start(); }
                _tb.Focus();
            }
            timmer.Start();
        }
        #endregion

        #region 下移健
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!_bEnable) { return; }
            if (sender == null) { return; }
            if (sender.GetType() != typeof(TextBox)) { return; }
            if (_listbox == null) { return; }
            var tb = (TextBox)sender;
            #region 下移动
            if (e.KeyCode == Keys.Down)
            { _listbox.Focus(); _listbox.SelectedIndex = 0; return; }
            #endregion
        }
        #endregion

        #region 得到焦点
        private void textboxGotFocus(object sender, EventArgs e)
        {
            if (!_bEnable) { return; }
            //_listbox.Items.Clear();
            _listbox.Left = _tb.Left;
            _listbox.Top = _tb.Top + _tb.Height + 5;
            _listbox.Width = _tb.Width;
            _listbox.Height = _tb.Height * 5;
            _listbox.Visible = true;
            _listbox.BringToFront();
            timmer.Start();
        }
        #endregion

        #region 失去焦点
        private void textboxLostFocus(object sender, EventArgs e)
        {
            if (!_bEnable) { return; }
            timmer.Stop();
            if (!_listbox.Focused & _listbox.Visible == true)
            {  _listbox.Visible = false; }
        }
        #endregion

        #region 鼠标点击，直接赋值
        private void listBox1_MouseClick(object sender, EventArgs e)
        {
            if (!_bEnable) { return; }
            if (sender == null) { return; }
            if (sender.GetType() != typeof(ListBox)) { return; }
            if (_listbox == null) { return; }
            #region 下移动
            _tb.Text = _listbox.Text;
            _listbox.Visible = false;
            SetCusVal(_tb.Text);
            #endregion
        }
        #endregion

        #region 键盘移动事件
        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!_bEnable) { return; }
            if (sender == null) { return; }
            if (sender.GetType() != typeof(ListBox)) { return; }
            if (e.KeyCode == Keys.Enter)
            {
                var cusname = _listbox.Text;
                _tb.Text = cusname; _listbox.Visible = false;
                SetCusVal(cusname);
            }
            if (e.KeyCode == Keys.Up)
            { if (_listbox.SelectedIndex == 0) { _tb.Focus(); } }
        }
        #endregion

        #region 设置客户档案的控件值
        public virtual void SetCusVal(string name)
        {
            var url = string.Format("{0}/api/oms/Customer/GetOneCusByName?token={1}&name={2}",
                ServerConfig.ServerAddress, ServerConfig.GetToken(), name);
            var s = Common.handleHttp.Get(url);
            var result = Common.handleJSON.DeserializeResult(s);
            if (result.result == -1) { return; }
            var list = result.GetList<Model.Customer>("Customer");
            if (list.Count > 0)
            {
                var cus = list[0];
                SetControlVal("TaxNo", cus.TaxNo);
                SetControlVal("Address", cus.Address);
                SetControlVal("Phone", cus.Phone);
                SetControlVal("Bank", cus.Bank);
                SetControlVal("BankAccount", cus.BankAccount);
            }
        }
        #endregion

        #region 设置控件值
        public virtual void SetControlVal(string controlname, string val)
        {
            var cc = _main.Controls.Find(controlname, true);
            if (cc.Count() > 0)
            {
                var c = (TextBox)cc[0];
                c.Text = val;
            }
        }
        #endregion

    }
}
